/// **************************************************************************************************
/// "Copyright (c) 2007 Sai Panyam, Inc.  All rights reserved."
/// **************************************************************************************************
/// Component Name   :	ListModifier, Member of System72.NTextile 
/// Description      :	ListModifier description
/// Author           :	Sai Panyam
/// Creation Date    :	November 09 2007
/// Modified Date    :              
/// Modified By      :
/// **************************************************************************************************
using System;
using System.Text.RegularExpressions;
using System72.NTextile.RegexLibrary;

namespace System72.NTextile
{
    /// <summary>
    /// 
    /// </summary>
    internal class ListModifier : IModifier
    {
        #region Private Variables

        private readonly Operation m_operation = new Operation();
        private readonly ListModifierType m_listModifierType;
        private AttributeCollection m_supportedAttributes;
        private PunctuationCollection m_supportedPunctuations;
        private bool m_success;
        private readonly int m_parseOrder=5;
        private readonly ModifierType m_type;
        
        #endregion

        #region Constructors
        /// <summary>
        /// 
        /// </summary>
        /// <param name="listModifierType"></param>
        public ListModifier(ListModifierType listModifierType)
        {
            m_listModifierType = listModifierType;
            OperationFactory factory = new OperationFactory(m_listModifierType);
            m_operation = factory.GetListOperation();
            m_type = m_operation.ModifierType;
        }

        #endregion

        #region Public Properties
        /// <summary>
        /// 
        /// </summary>
        public AttributeCollection SupportedAttributes
        {
            get
            {
                return m_supportedAttributes;
            }
            set
            {
                m_supportedAttributes = value;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public PunctuationCollection SupportedPunctuations
        {
            get { return m_supportedPunctuations; }
            set { m_supportedPunctuations = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public bool Success
        {
            get { return m_success; }
            set { m_success = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int ParseOrder
        {
            get { return m_parseOrder; }
        }
        /// <summary>
        /// 
        /// </summary>
        public ModifierType Type
        {
            get { return m_type; }
        }

        #endregion
                
        #region Public Methods
        /// <summary>
        /// 
        /// </summary>
        /// <param name="contentText"></param>
        /// <param name="excludedModifiers"></param>
        /// <returns></returns>
        public string GetXHTML(string contentText, ModifierCollection excludedModifiers)
        {
            string listTag = String.Empty;
            if (m_operation!=null)
            {
                if (!excludedModifiers.ContainsModifier(this))
                {
                    ListRegex listRegex=new ListRegex(m_operation.RegExOperator);
                    MatchCollection matchCollection = listRegex.GetMatches(contentText);
                    if (matchCollection.Count>0)
                    {
                        //Since there is atleast one match then we can set Success to true
                        m_success = true;
                        if (m_listModifierType==ListModifierType.NumericList)
                        {
                            listTag = "ol";
                        }
                        if (m_listModifierType == ListModifierType.BulletedList)
                        {
                            listTag = "ul";
                        }
                    }
                    int matchCount = 0;//Used to apply the attribute to only the first list element
                    //The attributes for the first list item will be applied to the whole list
                    string finalAttribute = String.Empty;
                    // The list string
                    string listXHTML = string.Empty;
                    foreach (Match match in matchCollection)
                    {
                        //Individual items can have attributes specific to them.
                        string listItemAttribute = String.Empty;
                        matchCount++; //Increment match count
                        string matchText = match.Value.Trim();//Remove any extra leading or trailing spaces
                        //We parse for attributes for all list items only when we find the attribute string just
                        //after the operator. i.e. in Group 2
                        if(match.Groups[3].Value!=String.Empty)
                        {
                            string attributeText = match.Groups[3].Value;
                            matchText = parseForAttributes(matchText, ref listItemAttribute, attributeText);
                        }
                        matchText = matchText.Substring(1);//Remove the list operator which will always be the first character for lists
                        if (matchCount == 1) //The first match sets the top most attributes.
                        {
                            finalAttribute = listItemAttribute;
                        }
                        matchText = Util.GetElementMarkUp(matchText, m_operation.Tag, listItemAttribute);
                        //Do the HTML only if any content is present between the operators
                        if (match.Value.Length > (m_operation.Operator.Length*2))
                        {
                            listXHTML += matchText;
                            // remove the list item from the original string
                            //we will put it back later with the listXHTML
                            //this is to ensure that we have valid XHTML which shouldn't contain any text between the main list tags (ol and ul)
                            contentText = contentText.Replace(match.Value, string.Empty);
                        }
                    }
                    if (listTag != String.Empty)
                    {
                        //The content text will now contain only extraneous text that isn't a part of the list markup.
                        contentText = contentText + Util.GetElementMarkUp(listXHTML, listTag, finalAttribute);
                    }
                }
            }
            return contentText;
        }

        

        #endregion

        #region Private Methods
        /// <summary>
        /// 
        /// </summary>
        /// <param name="matchText"></param>
        /// <param name="attributeString"></param>
        /// <param name="attributeText"></param>
        /// <returns></returns>
        private string parseForAttributes(string matchText, ref string attributeString, string attributeText)
        {
            if (m_supportedAttributes != null && m_supportedAttributes.Count > 0)
            {
                foreach (Attribute attribute in m_supportedAttributes)
                {
                    string matchValue;
                    string attributeValue = m_operation.GetAttributeText(attributeText, attribute, out matchValue);
                    attribute.AttributeValue = attributeValue;
                    string attributeKeyValue = attribute.GetAttributeKeyValueText();
                    if (matchValue == String.Empty)
                    {
                        attributeKeyValue = String.Empty;
                    }
                    else
                    {
                        matchText = matchText.Replace(matchValue, "");
                    }
                    attributeString = attributeString + attributeKeyValue;
                }
            }
            return matchText;
        }

        #endregion
        
    }
}
